/*
 * @Author: huangkaicheng@rfchina.com
 * @Date: 2020-09-08 16:54:02
 * @LastEditTime: 2020-09-16 14:43:04
 * @LastEditors: Please set LastEditors
 * @Description: Compress Image
 * @FilePath: \rf-auto-jenkins-plugin\lib\imgMin.js
 */
const inquirer = require('inquirer')
const mpci = require('../uploadMini/mp-cli.js') // npm i mp-ci -D
const path = require('path')

const {
  resolve,
  log,
  successLog,
  errorLog,
  showLoading,
  hideLoading,
} = require('../utils')
module.exports = async function autoUploadMini() {
  const answers = await inquirer
    .prompt([
      {
        type: 'input',
        name: 'version',
        message: '请输入版本号',
        validate: function (input) {
          var done = this.async()
          if (input == '') {
            done('版本号不能为空')
            return
          }
          if (/[^\d^\.]+/.test(input)) {
            done('版本号必须为数字')
            return
          }
          done(null, true)
        },
      },
      {
        type: 'input',
        name: 'describe',
        message: '请输入版本描述(可为空)',
        default: '',
      },
    ])
    .then(async function (res) {
      let { version, describe } = res

      console.log(res)
      showLoading('上传中···')
      let mpciInstance = new mpci({
        version: `${version}`,
        desc: `${describe}`,
        workspace: path.join(process.cwd()), //项目地址,不填则默认为当前脚本所在根目录
        pkp: path.join(process.cwd(), `private.key`), //代码上传秘钥,默认就是private.key这个名字,使用前需要使用小程序管理员身份访问"微信公众平台-开发-开发设置"后下载代码上传密钥
      })
      mpciInstance.upload({})
      hideLoading()
    })
}
